package com.msc.service.guestmessage.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msc.common.lang.exception.CiliciliErrorCodeEnum;
import com.msc.common.lang.exception.CiliciliException;
import com.msc.controller.guestmessage.vo.GuestMsgVo;
import com.msc.entity.guestmessage.GuestMessage;
import com.msc.mapper.guestmessage.GuestMessageMapper;
import com.msc.service.guestmessage.GuestMessageService;
import com.msc.util.DateUtil;
import com.msc.util.IpAddressUtils;
import com.msc.util.UserAgentUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author masc
 * @date 2022-07-17 1:11
 * <p></p>
 */
@Service
public class GuestMessageServiceImpl extends ServiceImpl<GuestMessageMapper, GuestMessage> implements GuestMessageService {
    @Autowired
    private GuestMessageMapper guestMessageMapper;
    @Autowired
    UserAgentUtils userAgentUtils;

    @Override
    public boolean createMsg(HttpServletRequest request, GuestMsgVo guestMsgVo) {
        GuestMessage guestMessage = new GuestMessage();
        BeanUtils.copyProperties(guestMsgVo, guestMessage);
        guestMessage.setCreateTime(LocalDateTime.now());
        guestMessage.setUpdateTime(LocalDateTime.now());
        guestMessage.setStatus(1);

        //设置访问信息
        //获取用户IP
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        //以上两种方式都获取不到，那就使用前端传过来的
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            // 前端通过搜狐API获取ip放到header里面
            ip = request.getHeader("yourIp");
        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
        if (null == ip) {
            //如果Ip为空，不让进行操作
            throw new CiliciliException(CiliciliErrorCodeEnum.NOT_IP);
        }
        String ipSource = IpAddressUtils.getCityInfo(ip);
        String userAgent = request.getHeader("User-Agent");
        Map<String, String> userAgentMap = userAgentUtils.parseOsAndBrowser(userAgent);
        String os = userAgentMap.get("os");
        String browser = userAgentMap.get("browser");
        guestMessage.setIp(ip);
        guestMessage.setIpSource(ipSource);
        guestMessage.setOs(os);
        guestMessage.setBrowser(browser);
        //判断当前ip下，今日留言数量是否大于2，大于不让操作
        QueryWrapper<GuestMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GuestMessage::getIp, ip)
                .ge(GuestMessage::getCreateTime, DateUtil.getDateStart()) //大于今天的00:00
                .le(GuestMessage::getCreateTime, DateUtil.getDateEnd());   //小于今天的23：59

        int count = this.count(queryWrapper);
        if (count > 2) {
            return false;
        }
        int insertNums = guestMessageMapper.insert(guestMessage);
        return insertNums > 0;
    }

}
